﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace warcaby
{
    public enum type
    {
        EmptyW,
        Empty,
        Black,
        BlackQueen,
        White,
        WhiteQueen
        
    }
    class Move
    {
        public Move(int X1, int Y1, int X2, int Y2)
        {
            x1 = X1;
            x2 = X2;
            y1 = Y1;
            y2 = Y2;
        }
        public Move()
        {
            x1 = y1 = x2 = y2 = -1;
        }
        public Move(int X1, int Y1)
        {
            x1 = X1;
            y1 = Y1;
            y2 = x2 = -1;
        }
        public int x1;
        public int y1;

        public int x2;
        public int y2;

    }
    class Map
    {
        private type[,] area = new type[8, 8];
        private string turn = "white";//"black" or "white"
        private int withoutslaughter=0;//how many without beating
        private Stack<Move> Moves=new Stack<Move>();
        private int movenr = 0;
        public int Withoutslaughter
        {
            get
            {
                return withoutslaughter;
            }
        }
        public int Movenr
        {
            get
            {
                return movenr;
            }
        }
        public string Turn
        {
            get
            {
                return turn;
            }
        }
        public Map()
        {
            for (int x = 0; x < 8; x = x + 2)
                for (int y = 0; y < 8; y = y + 2)
                    area[x, y] = type.Empty;
            for (int x = 1; x < 8; x = x + 2)
                for (int y = 1; y < 8; y = y + 2)
                    area[x, y] = type.Empty;

            for (int x = 1; x < 8; x = x + 2)
                for (int y = 0; y < 8; y = y + 2)
                    area[x, y] = type.EmptyW;
            for (int x = 0; x < 8; x = x + 2)
                for (int y = 1; y < 8; y = y + 2)
                    area[x, y] = type.EmptyW;

            for (int x = 0; x < 3; x = x + 2)
                for (int y = 0; y < 8; y = y + 2)
                    area[x, y] = type.Black;
            for (int x = 1; x < 3; x = x + 2)
                for (int y = 1; y < 8; y = y + 2)
                    area[x, y] = type.Black;


            for (int x = 5; x < 8; x = x + 2)
                for (int y = 1; y < 8; y = y + 2)
                    area[x, y] = type.White;
            for (int x = 6; x < 8; x = x + 2)
                for (int y = 0; y < 8; y = y + 2)
                    area[x, y] = type.White;

            

        }

        //not used:
        private int validate(int x, int y)
        {
            if (x < 0 || y < 0 || x > 7 || y > 7)
                return -1;
            return 0;

        }

        //tell what is in area (x,y)
        public type GetArea(int x, int y)
        {
            if (validate(x, y) == -1)
                return type.EmptyW;
            return area[x, y];
        }

        //not used:
        public int MoveCheck(Move move)
        {
            int x1 = move.x1;
            int y1 = move.y1;
            int x2 = move.x2;
            int y2 = move.y2;
            int error = -1;
            int possible = 1;
            type t = GetArea(x1, y1);            
            //sprawdzamy czy nie wychodzi za pole gry
            if (validate(x1, y1) == -1 || validate(x2, y2) == -1)
                return error;
            if (area[x2, y2] != type.Empty)
                return error;
            int stepx = x2 - x1;
            int stepy = y2 - y1;
            if (turn == "white")
            {
                if (t != type.White && t != type.WhiteQueen)
                    return error;
            }
            else if (turn == "black")
            {
                if (t != type.BlackQueen && t != type.Black)
                    return error;
            }
                //sprawdzamy dla czarnych
                if (t == type.Black)
                {

                    if ((x2 == x1 + 1) && (y2 == y1 + 1 || y2 == y1 - 1))
                        return possible;
                    else if ((x2 == x1 + 2 || x2 == x1 - 2) && (y2 == y1 + 2 || y2 == y1 - 2) && (area[x1 + stepx / 2, y1 + stepy / 2] == type.White || area[x1 + stepx / 2, y1 + stepy / 2] == type.WhiteQueen))
                        return possible;

                    else
                        return error;
                }
            //czarna królowa
            if ((stepx == stepy || stepx == -stepy) && t == type.BlackQueen)
            {
                int sx, sy;

                sx = 1;
                sy = stepy / stepx;
                if (stepx > 0)
                    for (int i = 0; i < stepx; i++)
                    {
                        if (area[x1 + sx * i, y1 + sy * i] != type.Empty && area[x1 + sx * (i + 1), y1 + sy * (i + 1)] != type.Empty)
                            return error;
                    }

                else
                    for (int i = 0; i > stepx; i--)
                    {
                        if (area[x1 + sx * i, y1 + sy * i] != type.Empty && area[x1 + sx * (i - 1), y1 + sy * (i - 1)] != type.Empty)
                            return error;
                    }
                return possible;
            }



            //sprawdzamy dla białych
            if (t == type.White)
            {


                if (x2 == x1 - 1 && (y2 == y1 + 1 || y2 == y1 - 1))
                    return possible;


                else if ((x2 == x1 + 2 || x2 == x1 - 2) && (y2 == y1 + 2 || y2 == y1 - 2) && (area[x1 + stepx / 2, y1 + stepy / 2] == type.Black || area[x1 + stepx / 2, y1 + stepy / 2] == type.BlackQueen))
                    return possible;
                else
                    return error;
                         
            }
            //biała kórowa
            if ((stepx == stepy || stepx == -stepy) && t == type.WhiteQueen)
            {
                int sx, sy;

                sx = 1;
                sy = stepy / stepx;
                if (stepx > 0)
                    for (int i = 0; i < stepx; i++)
                    {
                        if (area[x1 + sx * i, y1 + sy * i] != type.Empty && area[x1 + sx * (i + 1), y1 + sy * (i + 1)] != type.Empty)
                            return error;
                    }
                else
                    for (int i = 0; i > stepx; i--)
                    {
                        if (area[x1 + sx * i, y1 + sy * i] != type.Empty && area[x1 + sx * (i - 1), y1 + sy * (i - 1)] != type.Empty)
                            return error;
                    }
               
                return possible;
            }

            
            return error;
            
        }

        //make move
        public int Move(Move move)
        {
            
            int x1 =  move.x1;
            int y1 = move.y1;
            int x2 = move.x2; 
            int y2 = move.y2;
            int nrofbeaten = 0;
            nrofbeaten = GetBlack() - GetWhite();
            int error = -1;
            Stack<Move> Moves = new Stack<Move>();
            Moves = GetAllMoves();
            while (Moves.Count != 0)
            {
                Move m = Moves.Pop();
                if (move.x1 == m.x1 && move.x2 == m.x2 && move.y1 == m.y1 && move.y2 == m.y2)
                    break;
                else if (Moves.Count == 0)
                    return error;
            }

          //  if (MoveCheck(move) == -1)
            //    return error;
            movenr++;
            int stepx=(x2-x1);
            int stepy=(y2-y1);
            type t = GetArea(x1, y1);
            int sx, sy;

            sx = 1;
            sy = stepy / stepx;
            
            if(stepx>0)
             for (int i = 0; i < stepx; i++)
               area[x1 + sx * i, y1 + sy * i] = type.Empty;
            else
                for (int i = 0; i > stepx; i--)
                    area[x1 + sx * i, y1 + sy * i] = type.Empty;

             area[x2, y2] = t;

            nrofbeaten = nrofbeaten - (GetBlack() - GetWhite());


            if (nrofbeaten > 0)
                withoutslaughter = 0;
            else
                withoutslaughter++;
             if (see(x2, y2) == 1&&nrofbeaten!=0)
                return 1;

            if (t == type.Black && x2 == 7)
                t = type.BlackQueen;
            if (t == type.White && x2 == 0)
                t = type.WhiteQueen;
            area[x2, y2] = t;
            Moves.Clear();
            if(turn=="white")
                turn="black";
            else
                turn="white";
            return 1;
        }


        private int see(int x, int y)
        {
            //leftup
            if (GetArea(x, y) == type.White)
                if (x > 1 && y > 1 && (GetArea(x - 1, y - 1) == type.Black || GetArea(x - 1, y - 1) == type.BlackQueen) && GetArea(x - 2, y - 2) == type.Empty)
                    return 1;
            if (GetArea(x, y) == type.Black)
                if (x > 1 && y > 1 && (GetArea(x - 1, y - 1) == type.White || GetArea(x - 1, y - 1) == type.WhiteQueen) && GetArea(x - 2, y - 2) == type.Empty)
                    return 1;
            //leftdown
            if (GetArea(x, y) == type.White)
                if (x > 1 && y < 6 && (GetArea(x - 1, y + 1) == type.Black || GetArea(x - 1, y + 1) == type.BlackQueen) && GetArea(x - 2, y + 2) == type.Empty)
                    return 1;
            if (GetArea(x, y) == type.Black)
                if (x > 1 && y < 6 && (GetArea(x - 1, y +1) == type.White || GetArea(x - 1, y + 1) == type.WhiteQueen) && GetArea(x - 2, y + 2) == type.Empty)
                    return 1;
            //rightup
            if (GetArea(x, y) == type.White)
                if (x < 6 && y > 1 && (GetArea(x + 1, y - 1) == type.Black || GetArea(x + 1, y - 1) == type.BlackQueen) && GetArea(x + 2, y - 2) == type.Empty)
                    return 1;
            if (GetArea(x, y) == type.Black)
                if (x < 6 && y > 1 && (GetArea(x + 1, y - 1) == type.White || GetArea(x + 1, y - 1) == type.WhiteQueen) && GetArea(x + 2, y - 2) == type.Empty)
                    return 1;
            //rightdown
            if (GetArea(x, y) == type.White)
                if (x <6 && y <6 && (GetArea(x + 1, y + 1) == type.Black || GetArea(x + 1, y + 1) == type.BlackQueen) && GetArea(x + 2, y + 2) == type.Empty)
                    return 1;
            if (GetArea(x, y) == type.Black)
                if (x <6 && y <6 && (GetArea(x + 1, y + 1) == type.White || GetArea(x + 1, y + 1) == type.WhiteQueen) && GetArea(x + 2, y + 2) == type.Empty)
                    return 1;
            if (GetArea(x, y) == type.BlackQueen || GetArea(x, y) == type.WhiteQueen)
            {
                //leftup
                for(int i=0; i<6;i++)
                    if (GetArea(x, y) == type.WhiteQueen)
                    {
                        if (x -i<= 1 || y-i <= 1 ||(GetArea(x - i-1, y - i-1) != type.Empty && GetArea(x - i-2, y - i-2) != type.Empty))
                            break;
                        if ((GetArea(x - i - 1, y - i - 1) == type.Black || GetArea(x - i - 1, y - i - 1) == type.BlackQueen) && GetArea(x - i - 2, y - i - 2) == type.Empty)
                            return 1;
                    }
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.BlackQueen)
                                            {
                        if (x -i<= 1 || y-i <= 1 ||(GetArea(x - i-1, y - i-1) != type.Empty && GetArea(x - i-2, y - i-2) != type.Empty))
                            break;
                        if ((GetArea(x - i - 1, y - i - 1) == type.White || GetArea(x - i - 1, y - i - 1) == type.WhiteQueen) && GetArea(x - i - 2, y - i - 2) == type.Empty)
                            return 1;
                    }
                }
                //leftdown
                for (int i = 0; i < 6; i++)
                {
               
                    if (GetArea(x, y) == type.WhiteQueen)
                       {
                          if (x - i <= 1 || y + i >= 6 || (GetArea(x - i - 1, y + i + 1) != type.Empty && GetArea(x - i - 2, y + i + 2) != type.Empty))
                            break;
                          if ((GetArea(x - i - 1, y + i + 1) == type.Black || GetArea(x - i - 1, y + i + 1) == type.BlackQueen) && GetArea(x - i - 2, y + i + 2) == type.Empty)
                            return 1;
                    }
                }
                for (int i = 0; i < 6; i++)
                {
                  
                    if (GetArea(x, y) == type.BlackQueen)
                        {
                            if (x - i <= 1 || y + i >= 6 || (GetArea(x - i - 1, y + i + 1) != type.Empty && GetArea(x - i - 2, y + i + 2) != type.Empty))
                            break;
                            if ((GetArea(x - i - 1, y + i + 1) == type.White || GetArea(x - i - 1, y + i + 1) == type.WhiteQueen) && GetArea(x - i - 2, y + i + 2) == type.Empty)
                            return 1;
                    }
                }
                //rightup
                for (int i = 0; i < 6; i++)
                {
                       
                   if (GetArea(x, y) == type.WhiteQueen)
                    {
                      if (x + i >= 6 || y - i <= 1 || (GetArea(x + i + 1, y - i - 1) != type.Empty && GetArea(x + i + 2, y - i - 2) != type.Empty))
                            break;
                      if ((GetArea(x + i + 1, y - i - 1) == type.Black || GetArea(x + i + 1, y - i - 1) == type.BlackQueen) && GetArea(x + i + 2, y - i - 2) == type.Empty)
                            return 1;
                    }
                }
                for (int i = 0; i < 6; i++)
                {
                    
                   if (GetArea(x, y) == type.BlackQueen)
                   {
                       if (x + i >= 6 || y - i <= 1 || (GetArea(x + i + 1, y - i - 1) != type.Empty && GetArea(x + i + 2, y - i - 2) != type.Empty))
                            break;
                       if ((GetArea(x + i + 1, y - i - 1) == type.White || GetArea(x + i + 1, y - i - 1) == type.WhiteQueen) && GetArea(x + i + 2, y - i - 2) == type.Empty)
                            return 1;
                    }
                }
                //rightdown
                for (int i = 0; i < 6; i++)
                {
                  
                    if (GetArea(x, y) == type.WhiteQueen)
                    {
                        if (x + i >=6 || y + i >=6 || (GetArea(x + i + 1, y + i + 1) != type.Empty && GetArea(x + i + 2, y + i + 2) != type.Empty))
                            break;
                        if ((GetArea(x + i + 1, y + i + 1) == type.Black || GetArea(x + i + 1, y + i + 1) == type.BlackQueen) && GetArea(x + i + 2, y + i + 2) == type.Empty)
                            return 1;
                    }
                }
                for (int i = 0; i < 6; i++)
                {
                    
                    if (GetArea(x, y) == type.BlackQueen)
                    {
                        if ((x + i >=6 || y + i >=6) || (GetArea(x + i + 1, y + i + 1) != type.Empty && GetArea(x + i + 2, y + i + 2) != type.Empty))
                            break;
                        if ((GetArea(x + i+1, y + i+1) == type.White || GetArea(x + i+1, y + i+1) == type.WhiteQueen) && GetArea(x + i + 2, y + i + 2) == type.Empty)
                            return 1;
                    }
                }

            }
            return 0;

        }

        //getall without beating
        private Stack<Move> getall2(int x, int y)
        {
            Stack<Move> Moves = new Stack<Move>();
            //leftup
                    
            if (x > 0 && y > 0 && GetArea(x - 1, y - 1) == type.Empty&&GetArea(x,y)==type.White)
                Moves.Push(new Move(x, y, x - 1, y - 1));
            //leftdown
            if (x > 0 && y < 7 && GetArea(x - 1, y + 1) == type.Empty && GetArea(x, y) == type.White)
                Moves.Push(new Move(x, y, x - 1, y + 1));
            //rightup
            if (x < 7 && y > 0 && GetArea(x + 1, y - 1) == type.Empty && GetArea(x, y) == type.Black)
                Moves.Push(new Move(x, y, x + 1, y - 1));
            //rightdown
            if (x < 7 && y < 7 && GetArea(x + 1, y + 1) == type.Empty && GetArea(x, y) == type.Black)
                Moves.Push(new Move(x, y, x + 1, y + 1));
            if ((GetArea(x, y) == type.WhiteQueen || GetArea(x, y) == type.BlackQueen))
            {
                for (int i = 1; i < 8 && x - i >= 0 && y - i >= 0 && GetArea(x - i, y - i) == type.Empty; i++)
                     Moves.Push(new Move(x, y, x - i, y - i));

                for (int i = 1; i < 8 && x - i >= 0 && y + i < 8 && GetArea(x - i, y + i) == type.Empty; i++)
                     Moves.Push(new Move(x, y, x - i, y + i));

                for (int i = 1; i < 8 && x + i < 8 && y - i >= 0 && GetArea(x + i, y - i) == type.Empty; i++)
                     Moves.Push(new Move(x, y, x + i, y - i));

                for (int i = 1; i < 8 && x + i < 8 && y + i < 8 && GetArea(x + i, y + i) == type.Empty; i++)
                     Moves.Push(new Move(x, y, x + i, y + i));
            }
                return Moves;
 

       }
        //find all possible moves for specific pawn
        private Stack<Move> getall(int x, int y)
        {

            Stack<Move> Moves = new Stack<Move>();

            //leftup

            if (GetArea(x, y) == type.White)
                if (x > 1 && y > 1 && (GetArea(x - 1, y - 1) == type.Black || GetArea(x - 1, y - 1) == type.BlackQueen) && GetArea(x - 2, y - 2) == type.Empty)
                    Moves.Push(new Move(x,y,x-2,y-2));
            if (GetArea(x, y) == type.Black)
                if (x > 1 && y > 1 && (GetArea(x - 1, y - 1) == type.White || GetArea(x - 1, y - 1) == type.WhiteQueen) && GetArea(x - 2, y - 2) == type.Empty)
                                        Moves.Push(new Move(x,y,x-2,y-2));
            //leftdown
            if (GetArea(x, y) == type.White)
                if (x > 1 && y < 6 && (GetArea(x - 1, y + 1) == type.Black || GetArea(x - 1, y + 1) == type.BlackQueen) && GetArea(x - 2, y + 2) == type.Empty)
                                        Moves.Push(new Move(x,y,x-2,y+2));
            if (GetArea(x, y) == type.Black)
                if (x > 1 && y < 6 && (GetArea(x - 1, y + 1) == type.White || GetArea(x - 1, y + 1) == type.WhiteQueen) && GetArea(x - 2, y + 2) == type.Empty)
                                        Moves.Push(new Move(x,y,x-2,y+2));
            //rightup
            if (GetArea(x, y) == type.White)
                if (x < 6 && y > 1 && (GetArea(x + 1, y - 1) == type.Black || GetArea(x + 1, y - 1) == type.BlackQueen) && GetArea(x + 2, y - 2) == type.Empty)
                                        Moves.Push(new Move(x,y,x+2,y-2));
            if (GetArea(x, y) == type.Black)
                if (x < 6 && y > 1 && (GetArea(x + 1, y - 1) == type.White || GetArea(x + 1, y - 1) == type.WhiteQueen) && GetArea(x + 2, y - 2) == type.Empty)
                                       Moves.Push(new Move(x,y,x+2,y-2));
            //rightdown
            if (GetArea(x, y) == type.White)
                if (x < 6 && y < 6 && (GetArea(x + 1, y + 1) == type.Black || GetArea(x + 1, y + 1) == type.BlackQueen) && GetArea(x + 2, y + 2) == type.Empty)
                                        Moves.Push(new Move(x,y,x+2,y+2));
            if (GetArea(x, y) == type.Black)
                if (x < 6 && y < 6 && (GetArea(x + 1, y + 1) == type.White || GetArea(x + 1, y + 1) == type.WhiteQueen) && GetArea(x + 2, y + 2) == type.Empty)
                                        Moves.Push(new Move(x,y,x+2,y+2));
            if (GetArea(x, y) == type.BlackQueen || GetArea(x, y) == type.WhiteQueen)
            {
                //leftup
                for (int i = 0; i < 6; i++)
                    if (GetArea(x, y) == type.WhiteQueen)
                    {
                        if (x - i < 2 || y - i < 2 || (GetArea(x - i - 1, y - i - 1) != type.Empty && GetArea(x - i - 2, y - i - 2) != type.Empty) || GetArea(x - i - 1, y - i - 1) == type.White || GetArea(x - i - 1, y - i - 1) == type.WhiteQueen)
                            break;
                        if ((GetArea(x - i - 1, y - i - 1) == type.Black || GetArea(x - i - 1, y - i - 1) == type.BlackQueen) && GetArea(x - i - 2, y - i - 2) == type.Empty)
                        {
                            Moves.Push(new Move(x,y,x-i-2,y-i-2));
                            i++;
                            while(x - i -2> -1 && y - i -2> -1&&GetArea(x - i - 2, y - i - 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x-i-2,y-i-2));
                                i++;
                            }

                        }
                    }
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.BlackQueen)
                    {
                        if (x - i < 2 || y - i < 2 || (GetArea(x - i - 1, y - i - 1) != type.Empty && GetArea(x - i - 2, y - i - 2) != type.Empty) || GetArea(x - i - 1, y - i - 1) == type.Black || GetArea(x - i - 1, y - i - 1) == type.BlackQueen)
                            break;
                        if ((GetArea(x - i - 1, y - i - 1) == type.White || GetArea(x - i - 1, y - i - 1) == type.WhiteQueen) && GetArea(x - i - 2, y - i - 2) == type.Empty)
                                                    {
                            Moves.Push(new Move(x,y,x-i-2,y-i-2));
                            i++;
                            while(x - i -2> -1 && y - i -2> -1&&GetArea(x - i - 2, y - i - 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x-i-2,y-i-2));
                                i++;
                            }

                        }
                    }
                }
                //leftdown
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.WhiteQueen)
                    {
                        if (x - i < 2 || y + i > 5 || (GetArea(x - i - 1, y + i + 1) != type.Empty && GetArea(x - i - 2, y + i + 2) != type.Empty) || GetArea(x - i - 1, y + i + 1) == type.White || GetArea(x - i - 1, y + i + 1) == type.WhiteQueen)
                            break;
                        if ((GetArea(x - i - 1, y + i + 1) == type.Black || GetArea(x - i - 1, y + i + 1) == type.BlackQueen) && GetArea(x - i - 2, y + i + 2) == type.Empty)
                                                   {
                            Moves.Push(new Move(x,y,x-i-2,y+i+2));
                            i++;
                            while(x - i -2> -1 && y + i +2< 8&&GetArea(x - i - 2, y + i + 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x-i-2,y+i+2));
                                i++;
                            }

                        }
                    }
                }
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.BlackQueen)
                    {
                        if (x - i < 2 || y + i > 5 || (GetArea(x - i - 1, y + i + 1) != type.Empty && GetArea(x - i - 2, y + i + 2) != type.Empty) || GetArea(x - i - 1, y + i + 1) == type.Black || GetArea(x - i - 1, y + i + 1) == type.BlackQueen)
                            break;
                        if ((GetArea(x - i - 1, y + i + 1) == type.White || GetArea(x - i - 1, y + i + 1) == type.WhiteQueen) && GetArea(x - i - 2, y + i + 2) == type.Empty)
                                                    {
                            Moves.Push(new Move(x,y,x-i-2,y+i+2));
                            i++;
                            while(x - i -2> -1 && y + i +2< 8&&GetArea(x - i - 2, y + i + 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x-i-2,y+i+2));
                                i++;
                            }

                        }
                    }
                }
                //rightup
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.WhiteQueen)
                    {
                        if (x + i > 5 || y - i < 2 || (GetArea(x + i + 1, y - i - 1) != type.Empty && GetArea(x + i + 2, y - i - 2) != type.Empty) || GetArea(x + i + 1, y - i - 1) == type.White || GetArea(x + i + 1, y - i - 1) == type.WhiteQueen)
                            break;
                        if ((GetArea(x + i + 1, y - i - 1) == type.Black || GetArea(x + i + 1, y - i - 1) == type.BlackQueen) && GetArea(x + i + 2, y - i - 2) == type.Empty)
                                                        {
                            Moves.Push(new Move(x,y,x+i+2,y-i-2));
                            i++;
                            while(x + i +2<8 && y - i -2< 8&&GetArea(x + i + 2, y - i - 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x+i+2,y-i-2));
                                i++;
                            }

                        }
                    }
                }
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.BlackQueen)
                    {
                        if (x + i > 5 || y - i < 2 || (GetArea(x + i + 1, y - i - 1) != type.Empty && GetArea(x + i + 2, y - i - 2) != type.Empty) || GetArea(x + i + 1, y - i - 1) == type.Black || GetArea(x + i + 1, y - i - 1) == type.BlackQueen)
                            break;
                        if ((GetArea(x + i + 1, y - i - 1) == type.White || GetArea(x + i + 1, y - i - 1) == type.WhiteQueen) && GetArea(x + i + 2, y - i - 2) == type.Empty)
                                                    {
                            Moves.Push(new Move(x,y,x+i+2,y-i-2));
                            i++;
                            while(x + i +2<8 && y - i -2< 8&&GetArea(x + i + 2, y - i - 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x+i+2,y-i-2));
                                i++;
                            }

                        }
                    }
                }
                //rightdown
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.WhiteQueen)
                    {
                        if (x + i > 5 || y + i > 5 || (GetArea(x + i + 1, y + i + 1) != type.Empty && GetArea(x + i + 2, y + i + 2) != type.Empty) || GetArea(x + i + 1, y + i + 1) == type.White || GetArea(x + i + 1, y + i + 1) == type.WhiteQueen)
                            break;
                        if ((GetArea(x + i + 1, y + i + 1) == type.Black || GetArea(x + i + 1, y + i + 1) == type.BlackQueen) && GetArea(x + i + 2, y + i + 2) == type.Empty)
                                                    {
                            Moves.Push(new Move(x,y,x+i+2,y+i+2));
                            i++;
                            while(x + i +2<8 && y + i +2< 8&&GetArea(x + i + 2, y + i + 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x+i+2,y+i+2));
                                i++;
                            }

                        }
                    }
                }
                for (int i = 0; i < 6; i++)
                {

                    if (GetArea(x, y) == type.BlackQueen)
                    {
                        if ((x + i > 5 || y + i > 5) || (GetArea(x + i + 1, y + i + 1) != type.Empty && GetArea(x + i + 2, y + i + 2) != type.Empty) || GetArea(x + i + 1, y + i + 1) == type.Black || GetArea(x + i + 1, y + i + 1) == type.BlackQueen)
                            break;
                        if ((GetArea(x + i+1, y + i+1) == type.White || GetArea(x + i+1, y + i+1) == type.WhiteQueen) && GetArea(x + i + 2, y + i + 2) == type.Empty)
                                                    {
                            Moves.Push(new Move(x,y,x+i+2,y+i+2));
                            i++;
                            while(x + i +2<8 && y + i +2< 8&&GetArea(x + i + 2, y + i + 2) == type.Empty)
                            {
                                Moves.Push(new Move(x,y,x+i+2,y+i+2));
                                i++;
                            }

                        }
                    }
                }

            }
            return Moves;

        }


        public Map getNewMap(Map map, Move move)
        {
            Map maptmp = new Map();
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                    maptmp.area[x, y] = map.area[x, y];

            maptmp.withoutslaughter = map.Withoutslaughter;
            maptmp.turn = map.Turn;
            maptmp.MoveCp(move);
            return maptmp;
        }
        public double[] getArray()
        {
            double[] array = new double[34];



            int i = 0;
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                {
                    switch (area[x,y])
                    {
                        case type.Black:
                            array[i++] = 1;
                            break;
                        case type.BlackQueen:
                            array[i++] = 2;
                            break;
                        case type.White:
                            array[i++] = -1;
                            break;
                        case type.WhiteQueen:
                            array[i++] = -2;
                            break;
                        case type.Empty:
                            array[i++] = 0;
                            break;
                        default:
                            break;
                    }

                }
            array[32] = GetBlack() -GetWhite();
            array[33] = GetBlackQueen() - GetWhiteQueen();
            return array;
        }
        public Stack<Move> GetAllMoves()
        {
            Stack<Move> Moves = new Stack<Move>();
            Stack<Move> Pawns = new Stack<Move>();
            int howmany = 0;
            int howmanytmp;
            if(turn=="white")
                for (int x = 0; x < 8; x++)
                    for (int y = 0; y < 8; y++)
                    {
                        if (GetArea(x, y) == type.White || GetArea(x, y) == type.WhiteQueen)
                            Pawns.Push(new Move(x, y));
                    }
            if (turn == "black")
                for (int x = 0; x < 8; x++)
                    for (int y = 0; y < 8; y++)
                    {
                        if (GetArea(x, y) == type.Black || GetArea(x, y) == type.BlackQueen)
                            Pawns.Push(new Move(x, y));
                    }
            while (Pawns.Count != 0)
            {
                Move tmp = Pawns.Pop();
                howmanytmp = HowMany(tmp, Moves, howmany,0, this);

                if (howmany < howmanytmp)
                    howmany = howmanytmp;
                

            }

            return Moves;
        }
        int HowMany(Move move, Stack<Move> Moves,int hm, int level, Map map)
        {

            int ret,retmax=level;
            Stack<Move> AllMoves = map.getall(move.x1, move.y1);
            if (AllMoves.Count != 0)
                while (AllMoves.Count != 0)
                {
                    Move m = AllMoves.Pop();
                    ret = 0;
                    ret += HowMany(new Move(m.x2, m.y2), Moves, hm, level + 1, getNewMap(map, m));

                    if (ret + level == hm)
                        Moves.Push(m);
                    if (ret + level > hm)
                    {
                        Moves.Clear();
                        hm++;
                        Moves.Push(m);
                    }
                    if (retmax < ret)
                        retmax = ret;


                }
            else if (level == 0 && hm == 0)
            {
                AllMoves = map.getall2(move.x1, move.y1);
                while (AllMoves.Count != 0)
                {
                    Moves.Push(AllMoves.Pop());
                }
            }
            return retmax;
        }
        //move dla kopiowania, dla GetNewMap
        public int MoveCp(Move move)
        {
            int x1 = move.x1;
            int y1 = move.y1;
            int x2 = move.x2;
            int y2 = move.y2;

            int stepx = (x2 - x1);
            int stepy = (y2 - y1);
            type t = GetArea(x1, y1);
            int sx, sy;

            sx = 1;
            sy = stepy / stepx;

            if (stepx > 0)
                for (int i = 0; i < stepx; i++)
                    area[x1 + sx * i, y1 + sy * i] = type.Empty;
            else
                for (int i = 0; i > stepx; i--)
                    area[x1 + sx * i, y1 + sy * i] = type.Empty;

            area[x2, y2] = t; 

            if (see(x2, y2) == 1 && x2 - x1 != 1 && x1 - x2 != 1)
                return 1;

            if (t == type.Black && x2 == 7)
                t = type.BlackQueen;
            if (t == type.White && x2 == 0)
                t = type.WhiteQueen;
            area[x2, y2] = t;      

            Moves.Clear();
            if (turn == "white")
                turn = "black";
            else
                turn = "white";
            return 1;
        }
        //how many black there are:
        public int GetBlack()
        {
            int black = 0;
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                    if (area[x, y] == type.Black || area[x, y] == type.BlackQueen)
                        black++;
            return black;
        }
        //only queen
        public int GetBlackQueen()
        {
            int black = 0;
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                    if (area[x, y] == type.BlackQueen)
                        black++;
            return black;
        }
        //how many white there are:
        public int GetWhite()
        {
            int white = 0;
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                    if (area[x, y] == type.White || area[x, y] == type.WhiteQueen)
                        white++;
            return white;
        }
        //only queen
        public int GetWhiteQueen()
        {
            int white = 0;
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                    if ( area[x, y] == type.WhiteQueen)
                        white++;
            return white;
        }
        public Map CopyMap(Map map)
        {
            Map maptmp = new Map();
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                    maptmp.area[x, y] = map.area[x, y];
            maptmp.withoutslaughter = map.Withoutslaughter;
            maptmp.turn = map.Turn;
            
            return maptmp;
        }
   
    }
}
